package com.dxy.dxyuser.service.impl;


import com.dxy.dxycommon.VO.UserInfoVO;
import com.dxy.dxycommon.VO.UserRequestParam;
import com.dxy.dxycommon.utils.KeyUtil;
import com.dxy.dxyuser.DO.UserInfo;
import com.dxy.dxyuser.enums.ResultEnum;
import com.dxy.dxyuser.exception.UserException;
import com.dxy.dxyuser.repository.UserInfoRepository;
import com.dxy.dxyuser.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoRepository userInfoRepository;

    /**
     * 检验用户密码是否一致
     *
     * @param userInfoVO
     * @return
     */
    @Override
    public UserInfo checkUser(UserInfoVO userInfoVO) {
        UserInfo userInfo = userInfoRepository.findByUsername(userInfoVO.getUsername());
        String pwd = userInfo.getPassword();
        if (userInfoVO.getPassword().equals(pwd)) {
            return userInfo;
        }
        return null;
    }

    /**
     * 根据ID查询用户
     *
     * @param currentUserId
     * @return
     */
    @Override
    public UserInfo findById(String currentUserId) {
        Optional<UserInfo> userInfo = userInfoRepository.findById(currentUserId);
        return userInfo.get();
    }

    /**
     * 添加用户
     *
     * @param userInfoVO
     */
    @Override
    public void addUserInfo(UserInfoVO userInfoVO) {
        if (userInfoRepository.findByUsername(userInfoVO.getUsername()) == null) {
            UserInfo userInfo = new UserInfo();
            BeanUtils.copyProperties(userInfoVO, userInfo);
            userInfo.setId(KeyUtil.genUniqueKey());
            userInfoRepository.save(userInfo);
        } else {
            throw new UserException(ResultEnum.USER_EXIST);
        }
    }

    @Override
    public List<UserInfoVO> findUsers() {

        List<UserInfo> list = userInfoRepository.findAll();
        List<UserInfoVO> userInfoVOList = new ArrayList<>();
        for (UserInfo userInfo : list) {
            UserInfoVO userInfoVo = new UserInfoVO();
            BeanUtils.copyProperties(userInfo, userInfoVo);
            userInfoVOList.add(userInfoVo);
        }
        return userInfoVOList;
    }

    @Override
    public Page<UserInfo> findUsersNew(String username, String phone, String start1, String end1, Integer pageSize, Integer limit) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Pageable pa = new PageRequest(pageSize - 1, limit);
        Date start;
        Date end;
        Page<UserInfo> userInfos;
        try {
            start = sdf.parse(start1);
            end = sdf.parse(end1);
        } catch (ParseException e) {
            return userInfoRepository.findByUsernameLikeAndPhoneLike(username, phone, pa);
        } catch (NullPointerException e) {
            return userInfoRepository.findByUsernameLikeAndPhoneLike(username, phone, pa);
        }
        userInfos = userInfoRepository.findByUsernameLikeAndPhoneLikeAndCreateTimeBetween(username, phone, start, end, pa);
        return userInfos;
    }

    @Override
    public void deleteUser(String id) {
        userInfoRepository.deleteById(id);
    }

}
